package service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import model.Task;
import model.TaskRece;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

import dao.RoleDao;
import dao.TaskDao;
import dao.TaskReceDao;

@Service
public class TaskService {
	@Autowired
	private TaskDao taskDao;
	private Task task;
	@Autowired
	private TaskReceDao taskReceDao;
	private TaskRece taskRece;
	@Autowired
	private RoleDao roleDao;

	public void saveOrUpdate(Task task, String receids) {
		task.setLasttime(new Date());
		task.setIsread(0);// 初始状态（发出，未读）
		if (task.getId() == null) {
			taskDao.save(task);
		} else {
			taskDao.update(task);
		}
		Integer taskid = task.getId();
		if (StringUtils.isNotBlank(receids)) {
			String[] _receids = receids.split(",");
			// 删除
			taskReceDao.delete(task.getId());
			for (String receiver : _receids) {
				Integer receiverid = Integer.parseInt(receiver);
				TaskRece _taskRece = new TaskRece();
				_taskRece.setTaskid(taskid);
				_taskRece.setReceiverid(receiverid);
				taskReceDao.save(_taskRece);
			}
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public PageList<HashMap<String, Object>> queryPage(int page, int limit,
			HashMap<String, Object> queryMap) {
		Integer userid = (Integer) queryMap.get("temp");
		String part = (String) queryMap.get("part");
		List<HashMap<String, Object>> roles = roleDao.getRoles(userid);
		int roleFlag = 4; // 初始最低权限
		for (int i = 0; i < roles.size(); i++) {
			Integer roleid = (Integer) roles.get(i).get("roleid");
			/* 最高权限:开发、系统 业务管理 */
			if (roleid.equals(1) || roleid.equals(7) || roleid.equals(8)) {
				roleFlag = 1;
				break; // 最高权限，跳出循环
			}
			/* 市质监局 */
			else if (roleid.equals(9)) {
				roleFlag = 2;
			}
			/* 区质监局 */
			else if (roleid.equals(10)) {
				roleFlag = 3;
			}
			/* 社区管理员以及平民 */
			else {
				roleFlag = 4;
			}
		}
		PageList<HashMap<String, Object>> pagelist = new PageList<HashMap<String, Object>>();
		PageBounds pageBounds = new PageBounds(page, limit);
		ArrayList al = new ArrayList();
		Integer status_query = (Integer) queryMap.get("status_query");
		if (roleFlag == 1) {
			al.add(1);
			al.add(2);
			al.add(3); // 状态为1，2，3（除删除）
			if (status_query != null) {
				al.clear();
				queryMap.put("status", al.add(status_query));
			}
			queryMap.put("status", al);
			pagelist = (PageList<HashMap<String, Object>>) taskDao.queryPage(
					pageBounds, queryMap);
		} else if (roleFlag == 2) {
			queryMap.put("status", al);
			queryMap.put("owner", userid);
			if (StringUtils.isNotBlank(part) && part.equals("demo")) {
				// 个人工作台请求
				al.add(3);// 已反馈
				queryMap.put("isread", 2);// 区县已反馈，市局未读
				pagelist = (PageList<HashMap<String, Object>>) taskDao
						.queryPage4Demo(pageBounds, queryMap);
			} else {
				al.add(1);
				al.add(2);
				al.add(3); // 状态为1，2，3（除删除）
				if (status_query != null) {
					al.clear();
					queryMap.put("status", al.add(status_query));
				}
				pagelist = (PageList<HashMap<String, Object>>) taskDao
						.queryPage(pageBounds, queryMap);
			}
		} else if (roleFlag == 3) {
			al.add(2); // 状态为2（待反馈状态）
			al.add(3); // 状态为3（已反馈状态）
			queryMap.put("status", al);
			if (status_query != null) {
				al.clear();
				queryMap.put("status", al.add(status_query));
			}
			queryMap.put("userid", userid);
			pagelist = (PageList<HashMap<String, Object>>) taskDao.queryPage(
					pageBounds, queryMap);
		} else {
			al.add(2); // 状态为2（待反馈状态）
			al.add(3); // 状态为3（已反馈状态）
			queryMap.put("status", al);
			if (status_query != null) {
				al.clear();
				queryMap.put("status", al.add(status_query));
			}
			queryMap.put("userid", userid);
			pagelist = (PageList<HashMap<String, Object>>) taskDao.queryPage(
					pageBounds, queryMap);
		}
		return pagelist;
	}

	public void delete(String ids) {
		String[] _ids = ids.split(",");
		for (String id : _ids) {
			Integer taskid = Integer.parseInt(id);
			taskReceDao.delete(taskid);
			taskDao.delete(taskid);
		}
	}

	public void read(Integer id, String role) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("isread", 1);
		map.put("readtime", new Date());
		if (StringUtils.isNotBlank(role) && role.equals("quxian")) {
			map.put("isread", 1);// 区县 读取
		} else if (StringUtils.isNotBlank(role) && role.equals("quxian")) {
			map.put("isread", 3);// 市局 读取
		}
		taskDao.read(map);
	}

	public void submit(String ids) {
		String[] _ids = ids.split(",");
		for (String id : _ids) {
			Integer taskid = Integer.parseInt(id);
			HashMap<String, Object> taskinfo = new HashMap<String, Object>();
			taskinfo.put("id", taskid);
			taskinfo.put("sendtime", new Date());
			taskinfo.put("status", 2);
			taskDao.submit(taskinfo);
		}
	}

	public HashMap<String, Object> getById(Integer id) {
		return taskDao.getById(id);
	}

	public List<HashMap<String, Object>> getByTask(Integer taskid) {
		return taskReceDao.getByTask(taskid);
	}

	public void feedback(HashMap<String, Object> map) {
		taskDao.feedback(map);
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public TaskRece getTaskRece() {
		return taskRece;
	}

	public void setTaskRece(TaskRece taskRece) {
		this.taskRece = taskRece;
	}

}
